const db = require('./DB')


// 详情
const getHouseList = () => {
  return new Promise((resolve, reject) => {
    let sql = `SELECT * FROM kss`
    db.query(sql, (err, results) => {
      // console.log(err, results);
      if (err) {
        return reject(err)
      }
      resolve(results)
    })
  })
}
// getHouseList()




// 添加
const createHouse = (house) => {
  return new Promise((resolve, reject) => {
    let sql = "INSERT INTO kss(`title`, `description`, `price`, `area`, `bedrooms`, `bathrooms`, `address`, `city`, `state`, `zip_code`, `country`, `latitude`, `longitude`, `construction_year`, `property_type`, `ownership`, `amenities`, `status`) VALUES (?, ?,?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"
    db.query(sql,
      house,
      (err, results) => {
        if (err) {
          return reject(err)
        }
        resolve(results)
      })
  })
}



// 删除
const getdelect = (houses) => {
  return new Promise((resolve, reject) => {
    let sql = `DELETE FROM kss WHERE house_id = ${houses.query.id}`
    db.query(sql, (err, results) => {
      if (err) {
        return reject(err)
      }
      resolve(results)
    })
  })
}



// 修改
const getxiugai = (data) => {
  return new Promise((resolve, reject) => {
    let sql = `UPDATE kss SET title = ${data.query.title} , description = ${data.query.description} ,price = ${data.query.price} WHERE house_id = ${data.query.id}`
    db.query(sql,
      (err, results) => {
        if (err) {
          return reject(err)
        }
        resolve(results)
      })
  })
}



// 搜索
const getsous = (data) => {
  return new Promise((resolve, reject) => {
    let sql = `SELECT * FROM kss WHERE title LIKE "%${data.query.title}%" LIMIT 0,1000`;
    db.query(sql,
      (err, results) => {
        if (err) {
          return reject(err)
        }
        resolve(results)
      })
  })
}




module.exports = {
  createHouse,
  getHouseList,
  getdelect,
  getxiugai,
  getsous
}

